<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right"
                   style="font-size: 18px">
      <el-breadcrumb-item :to="{ path: '/' }">后台管理</el-breadcrumb-item>
      <el-breadcrumb-item>院系列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="院系" prop="facultyId">
        <el-select v-model="ruleForm.facultyId" placeholder="请选择院系">
          <el-option
              v-for="item in facultyListOptions"
              :key="item.id"
              :label="item.facultyName"
              :value="item.id">
          </el-option>
        </el-select>
        <span style="margin-left: 30px">
        <el-button type="primary" @click="submitForm('ruleForm')">查询</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
          </span>
      </el-form-item>
    </el-form>
    <el-table
        :data="list"
        border
        style="width: 100%">

      <el-table-column prop="facultyName" label="院系名称" width="150" align="center">
      </el-table-column>
      <el-table-column prop="establishedTime" label="成立时间" width="150" align="center">
      </el-table-column>
      <el-table-column prop="majorNum" label="专业数" width="100" align="center">
      </el-table-column>
      <el-table-column prop="studentNum" label="学生人数" width="100" align="center">
      </el-table-column>
      <el-table-column prop="teacherName" label="学院长" width="100" align="center">
      </el-table-column>
      <el-table-column label="操作" width="120" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="handleEdit(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>  <!--scope.row表示当前行-->
      </el-table-column>
    </el-table>

    <!--弹出的编辑院系的对话框-->
    <el-dialog title="修改院系" :visible.sync="dialogFormVisible">
      <el-form :model="ruleForm">
        <el-form-item label="院系名称" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.facultyName"></el-input>
        </el-form-item>
        <el-form-item label="成立时间" :label-width="formLabelWidth">
          <template>
            <el-date-picker
                v-model="ruleForm.establishedTime"
                align="right"
                type="date"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                placeholder="选择日期">
            </el-date-picker>
          </template>
        </el-form-item>
        <el-form-item label="专业数" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.majorNum" disabled></el-input>
        </el-form-item>
        <el-form-item label="学生人数" :label-width="formLabelWidth">
          <el-input v-model="ruleForm.studentNum" disabled></el-input>
        </el-form-item>
        <el-form-item label="学院长" :label-width="formLabelWidth">
          <el-select v-model="ruleForm.teacherId" placeholder="请选择">
            <el-option
                v-for="item in teacherListOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEdit()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      teacherListOptions: [],
      ruleForm: {
        facultyId: '',
        facultyName:'',
        establishedTime: '',
        majorNum:'',
        studentNum:'',
        teacherName: '',
        teacherId:''
      },
      facultyListOptions: [],
      tableData: [],
      list: [],
      dialogFormVisible: false,
      formLabelWidth: '120px'
    }
  },
  methods: {
    submitEdit() {
      let url = 'http://localhost:9801/faculties/' + this.ruleForm.id + '/update';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.ruleForm);
      console.log('formData = ' + formData);

      this.axios.post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '修改院系成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadFacultyList();
        } else if (responseBody.state == 40900) {
          this.$message.error(responseBody.message);
        } else {
          this.$message.error(responseBody.message);
          this.dialogFormVisible = false;
          this.loadFacultyList();
        }
      });
    },
    loadTeacherList() {
      console.log('loadTeacherList');
      let url = 'http://localhost:9800/teacher';
      console.log('url = ' + url);
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        this.teacherListOptions = responseBody.data;
      });
    },
    handleEdit(faculty) {
      let message = '您正在尝试编辑【' + faculty.id + '-' + faculty.facultyName + '】的院系详情……';
      console.log(message);
      console.log(faculty);
      let url = 'http://localhost:9801/faculties/' + faculty.id;
      console.log(url);
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        console.log(response.data)
        if (responseBody.state == 20000) {
          this.ruleForm = responseBody.data;
          console.log(responseBody.data);
          this.dialogFormVisible = true;
        } else {
          this.$message.error(responseBody.message);
          this.loadFacultyList();
        }
      });
    },
    handleDelete(faculty) {
      console.log('准备执行删除, id=' + faculty.id);
      let url = 'http://localhost:9801/faculties/' + faculty.id + '/delete';
      console.log('url=' + url);
      this.axios.post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state != 20000) {  //如果删除不成功则提示错误信息
          this.$message.error(responseBody.message);
        }
        this.loadFacultyList();  //刷新页面
      })
    },
    openDeleteConfirm(faculty) {
      let message = '此操作将永久删除【' + faculty.facultyName + '】院系, 是否继续?';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(faculty);
      }).catch(() => {
        //点击取消后的操作
      });
    },
    loadFacultyList() {
      console.log('loadFacultyList');
      let url = 'http://localhost:9801/faculties';
      console.log('url = ' + url);
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        this.facultyListOptions = responseBody.data;
        this.list = this.facultyListOptions;
      });
    },
    loadFacultyListById() {
      console.log('loadFacultyListById');
      let url = 'http://localhost:9801/faculties/list/' + this.ruleForm.facultyId;
      console.log('url = ' + url);
      this.axios.get(url).then((response) => {
        let responseBody = response.data;
        this.tableData = responseBody.data;
        console.log(this.tableData);
        this.list = this.tableData;
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {  /*对表单进行检查验证*/
        if (valid) {
          this.loadFacultyListById();
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.loadFacultyList();
    }
  },
  mounted() {  //挂载，在created后面执行的
    console.log('mounted');
    this.loadFacultyList();
    this.loadTeacherList();
  }
}
</script>
